﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace pryConvertTXT.Clases_Util
{
    public class CrackClave
    {

        private static string xCadCrack = "!ã1ka_Ãq&r%)#<s¿i?¡$@Âx:{b§u0(¥ ‼->.} ";
        private static string xCadEquiv = "ABCDEFGHIJKLMNLOPQRSTUVWXYZ0123456789 ";
        public static string Encryptar(string Text)
        {
            string resul = "";

            try
            {
                int i = 0;
                bool flag = true;
                for (i = 1; i <= Text.Length; i++)
                {
                    string letra = Mid(Text, i, 1);
                    int pos = xCadEquiv.IndexOf(letra);
                    if (flag == true)
                    {
                        pos = pos + 19;
                        if (pos > 37)
                        {
                            pos = pos - 37;
                        }
                        flag = false;
                    }
                    else
                    {
                        pos = pos - 19;
                        if (pos < 1)
                        {
                            pos = pos + 37;
                        }
                        flag = true;
                    }
                    resul = resul + xCaracter(2) + Mid(xCadCrack, pos, 1) + xCaracter(1);
                }
            }
            catch (Exception ex)
            {
                throw ex;
                //Interaction.MsgBox(ex.Message, MsgBoxStyle.Critical);
            }
            return resul;
        }
        public static string DesEncryptar(string Passw)
        {
            string resul = "";
            try
            {
                int i = 0;
                bool flag = true;
                if (Passw.Length == 0)
                {
                    Passw = "BLANCOERROR123";
                    // CUANDO EL PASSWORD ES ESTA EN BLANCO LA CONTRASEÑA ES QRQS
                }
                for (i = 1; i <= Passw.Length; i++)
                {
                    string letra = Mid(Passw, i + 2, 1);
                    int pos = xCadCrack.IndexOf(letra);
                    if (flag == true)
                    {
                        pos = pos - 19;
                        if (pos < 1)
                        {
                            pos = pos + 37;
                        }
                        flag = false;
                    }
                    else
                    {
                        pos = pos + 19;
                        if (pos > 37)
                        {
                            pos = pos - 37;
                        }
                        flag = true;
                    }
                    resul = resul + Mid(xCadEquiv, pos, 1);
                    i = i + 3;
                }

            }
            catch (Exception ex)
            {
                throw ex;
                //Interaction.MsgBox(ex.Message, MsgBoxStyle.Critical);
            }
            return resul;
        }
        private static string xCaracter(byte p)
        {
            Random random = new Random();
            string resul = "";
            try
            {
                int xPOS = (random.Next() * 10) * 3 + 10;
                xPOS = xPOS - (random.Next() * 10);
                if (xPOS < 1)
                {
                    xPOS = xPOS + (random.Next() * 10);
                }
                resul = Mid(xCadCrack, xPOS, p);
            }
            catch (Exception ex)
            {
                throw ex;
                //Interaction.MsgBox(ex.Message, MsgBoxStyle.Critical);
            }
            return resul;
        }

        public static string Mid(string param, int startIndex, int length)
        {
            string result = param.Substring(startIndex, length);
            return result;
        }

    }
}
